package com.zhs.gccs.adapter;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.zhs.gccs.BuildConfig;
import com.zhs.gccs.R;
import com.zhs.gccs.model.GccsTaskFile;
import com.zhs.gccs.utils.SharedPrefHelper;
import com.zhs.gccs.vo.CommplaintBean;

import java.util.ArrayList;

/**
 * Created by chengzf on 2024/7/21.
 */
public class ComplaintImageAdapter extends BaseQuickAdapter<GccsTaskFile, BaseViewHolder> {

    static int TYPE_DEFAULT = 101;
    static int TYPE_IMAGE = 102;

    private int max;
    private boolean isEdit = true;
    ArrayList<GccsTaskFile> list = new ArrayList<>();

    public ComplaintImageAdapter() {
        super(new ArrayList<>());
    }

    public int getMax() {
        return max;
    }

    public void setMax(int max) {
        this.max = max;
    }

    public boolean isEdit() {
        return isEdit;
    }

    public void setEdit(boolean edit) {
        isEdit = edit;
    }


    public void setComplaintData(ArrayList<GccsTaskFile> data) {
        // 查看的时候不能添加
        list.clear();
        list.addAll(data);
        /*if (list.size() < max){
            list.add(new CommplaintBean("", "", true));
        }*/
//        list.add(new CommplaintBean("", "", true));
        GccsTaskFile gccsTaskFile = new GccsTaskFile();
        gccsTaskFile.setDefault(true);
        list.add(gccsTaskFile);
        isEdit = true;
        super.setNewData(list);
    }


    @Override
    protected int getDefItemViewType(int position) {
        if (mData.get(position).isDefault()) {
            return TYPE_DEFAULT;
        } else {
            return TYPE_IMAGE;
        }
    }

    @Override
    protected BaseViewHolder onCreateDefViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_DEFAULT) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.item_bar_code_check_default_view, parent, false);
            return new NormalViewHolder(view);
        } else {
            View view = LayoutInflater.from(mContext).inflate(R.layout.item_bar_code_check_image_view, parent, false);
            return new ContractViewHolder(view);
        }
    }


    @Override
    protected void convert(@NonNull BaseViewHolder holder, GccsTaskFile bean) {
        if (holder instanceof ContractViewHolder) {
            holder.addOnClickListener(R.id.delete_cover);
            holder.setGone(R.id.delete_cover, isEdit);
            ImageView cover = holder.getView(R.id.cover);
//            Glide.with(mContext).load(bean.getImg()).into(cover);
            if (TextUtils.isEmpty(bean.getLocalUrl())) {
                String path = BuildConfig.BASEURL + "gccsFile/preview/image/" + bean.getId()
                        + "?token=" + SharedPrefHelper.getInstance().getToken();
                Glide.with(mContext).load(path).into(cover);
            } else {
                Glide.with(mContext).load(bean.getLocalUrl()).into(cover);
            }
        } else {

        }
    }

    class ContractViewHolder extends BaseViewHolder {

        public ContractViewHolder(View view) {
            super(view);
        }

    }

    class NormalViewHolder extends BaseViewHolder {

        public NormalViewHolder(View view) {
            super(view);
        }

    }
}
